# OS Environment
ARCH := $(shell uname -m)
CCACHE := $(shell which ccache 2>/dev/null)
CXX = ${CCACHE} g++
OS = $(shell uname -s)

# Flags
DEBUG_FLAGS = -g -pg
DEF_FLAGS = -D${ARCH} -pthread
OPT_FLAGS = -O3
WARN_FLAGS = -Wall

# Project
HEADERSDIR = ../include
LIBDIR = ../build/lib
LIBNAME = provident

TEST_HEADERS = ExpiryCalculatorTest.hpp \
               models/analytical/BlackScholesTest.hpp \
               models/binomial/BinomialTreeTest.hpp
TEST_HEADERSDIR = include
TEST_LIBDIR = build/lib
TEST_LIBNAME = testprovident
TEST_LIB = ${TEST_LIBDIR}/lib${TEST_LIBNAME}.a
TEST_LIB_PATH = -L${TEST_LIBDIR} -L${LIBDIR}
TEST_LIBS = -l${TEST_LIBNAME} -l${LIBNAME} -lpromote++ -pthread -lm
TEST_INCLUDE_PATH = -I${HEADERSDIR} -Isrc/cpp/
TEST_OBJDIR = build/obj
TEST_OBJS = ExpiryCalculatorTest.o \
            models/analytical/BlackScholesTest.o \
            models/binomial/BinomialTreeTest.o
TEST_SRCDIR = src/cpp

TEST_SRCDIR_HEADERS = ${addprefix ${TEST_SRCDIR}/, ${TEST_HEADERS}}
TEST_OBJDIR_OBJS = ${addprefix ${TEST_OBJDIR}/, ${TEST_OBJS}}

# Compiling
COMPILE = ${CXX} ${DEBUG_FLAGS} ${DEF_FLAGS} ${OPT_FLAGS} ${WARN_FLAGS} ${TEST_INCLUDE_PATH}
COMPILE_AND_LINK = ${COMPILE} ${TEST_LIB_PATH} ${TEST_LIBS} 

all: test

clean: 
	@echo -e "\033[7m[Cleaning...]\033[0m"
	rm -rf ${TEST_OBJDIR_OBJS} ${TEST_LIB} ${TEST_HEADERSDIR}/*

install: lib
	@echo -e "\033[7m[Installing...]\033[0m"
	cp ${TEST_SRCDIR_HEADERS} ${TEST_HEADERSDIR}

lib: compile_echo ${TEST_OBJDIR_OBJS}
	@echo -e "\033[7m[Creating library...]\033[0m"
	ar rcs ${TEST_LIB} ${TEST_OBJDIR_OBJS}

test: install
	@echo -e "\033[7m[Creating library...]\033[0m"
	${COMPILE} src/cpp/UnitTest.cpp -o build/unit-test ${TEST_LIB_PATH} ${TEST_LIBS}
	${COMPILE} src/cpp/models/analytical/PerfTest.cpp -o build/analytical-perf-test ${TEST_LIB_PATH} ${TEST_LIBS}
	${COMPILE} src/cpp/models/binomial/PerfTest.cpp -o build/binomial-perf-test ${TEST_LIB_PATH} ${TEST_LIBS}
	${COMPILE} src/cpp/custom/HardcodedTest.cpp -o build/hardcoded-test ${TEST_LIB_PATH} ${TEST_LIBS}

compile_echo:
	@echo -e "\033[7m[Compiling...]\033[0m"

${TEST_OBJDIR}/%.o: ${TEST_SRCDIR}/%.cpp ${TEST_SRCDIR_HEADERS} ${HEADERSDIR}/*
	${COMPILE} -c ${TEST_SRCDIR}/$*.cpp -o $@

.PHONY: all clean

